//
//  UITabBar+LZBadge.m
//  laziz_Consumer
//
//  Created by xbn_iMac on 2017/4/7.
//  Copyright © 2017年 XBN. All rights reserved.
//

#import "UITabBar+LZBadge.h"

#define TabbarItemNums 3.0    //tabbar的数量

@implementation UITabBar (LZBadge)

- (void)showBadgeOnItemIndex:(int)index{
    
    if ([LZLanguageManager shareInstance].getLanguageType == LanguageType_Ar) {
        index = abs(index+1-3);
    }

    //移除之前的小红点
    [self removeBadgeOnItemIndex:index];
    
    //新建小红点
    UIView *badgeView = [[UIView alloc]init];
    badgeView.tag = 888 + index;
    badgeView.layer.cornerRadius = 3.5;
    badgeView.backgroundColor = UIColorFromRGB(0xff0000, 1);
    CGRect tabFrame = self.frame;
    
    //确定小红点的位置
    CGFloat xx = (index*2+1)/(TabbarItemNums * 2);
    float percentX = xx * SCREEN_WIDTH + 9;
    
    CGFloat x = ceilf(percentX);
    CGFloat y = ceilf(0.23 * tabFrame.size.height);
    badgeView.frame = CGRectMake(x, y, 7, 7);
    [self addSubview:badgeView];
}

- (void)hideBadgeOnItemIndex:(int)index{
    
    //移除小红点
    if ([LZLanguageManager shareInstance].getLanguageType == LanguageType_Ar) {
        index = abs(index+1-3);
    }
    
    [self removeBadgeOnItemIndex:index];
    
}

- (void)removeBadgeOnItemIndex:(int)index{
    
    //按照tag值进行移除
    for (UIView *subView in self.subviews) {
        
        if (subView.tag == 888+index) {
            
            [subView removeFromSuperview];
            
        }
    }
}

@end
